package com.snap.vseries.ui;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.snap.vseries.R;
import com.snap.vseries.base.BaseActivity;
import com.snap.vseries.bean.ExpInfo;
import com.snap.vseries.fragment.NewExperimentInfoSettingFragment;
import com.snap.vseries.fragment.NewExperimentProgramSettingFragment;
import com.snap.vseries.fragment.NewExperimentStartFragment;
import com.snap.vseries.fragment.NewExperimentTemplateFragment;
import com.snap.vseries.manager.FileManager;
import com.snap.vseries.service.ExpController;
import com.snap.vseries.service.PcrService;
import com.snap.vseries.ui.widget.ExperimentProcessView;
import com.snap.vseries.ui.widget.TextButton;
import com.snap.vseries.utils.AutoUtil;
import com.snap.vseries.utils.TopTitleUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ExpNewExperimentActivity extends BaseActivity {
    public ExpInfo expInfo;
    public int index;
    @BindView(R.id.experiment_process_view)
    ExperimentProcessView experimentProcessView;
    List<String> experimentProcessList = new ArrayList<String>();
    private NewExperimentTemplateFragment templateFragment;
    private NewExperimentInfoSettingFragment informationFragment;
    private NewExperimentProgramSettingFragment programSettingFragment;
    private NewExperimentStartFragment startFragment;
    private int currentCheckedId = -1;
    private ExpController expController;

    @Override
    protected int layoutResId() {
        return R.layout.activity_new_experiment;
    }

    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            PcrService.PcrBinder binder = (PcrService.PcrBinder) service;
            expController = binder.getService().getCurrentController();
            onServiceBinded();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };

    private void onServiceBinded() {

    }

    @Override
    protected void onDestroy() {
        if (expController != null) {
//            mService.unregisterOnExpStateChangedListener(listener);
            expController = null;
            unbindService(connection);
        }
        super.onDestroy();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this, PcrService.class);
        bindService(intent, connection, BIND_AUTO_CREATE);
        initView();
        expInfo = new ExpInfo();
        expInfo.setName(AutoUtil.getFileName());
        //实验未完成前，虽然在本地做了存储。但是未收录到数据库，所以并不会显示
        FileManager.getInstance().saveExpData(expInfo);
        showFragment(0);
    }

    private void initView() {
        TextButton textView = new TextButton(this);
        textView.setText("退出");
        textView.setTextSize(16);
        textView.setTextColor(getResources().getColor(R.color.black666));
        textView.setClickable(false);
        new TopTitleUtils(this)
                .setTitle(getString(R.string.new_experiment))
                .addRightView(textView)
                .setRightViewVisiable(View.VISIBLE)
                .setRight(v -> {
                    HomeActivity.startActivity(ExpNewExperimentActivity.this);
                    finish();
                }).setLeft(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (index <= 0) {
                    ExpNewExperimentActivity.this.finish();
                } else {
                    ExpNewExperimentActivity.this.showFragment(index - 1);
                }
            }
        });
        experimentProcessList.add(getString(R.string.projects));
        experimentProcessList.add("基础设置");
        experimentProcessList.add("程序设置");
        experimentProcessList.add(getString(R.string.start));

        experimentProcessView.setStep(0).setExperimentProcess(experimentProcessList);
        experimentProcessView.post(new Runnable() {
            @Override
            public void run() {
                experimentProcessView.render();
            }
        });

    }


    public void showFragment(int checkedId) {
        if (currentCheckedId == checkedId) {
            return;
        }
        currentCheckedId = checkedId;
        //更新指示标志
        experimentProcessView.setStep(currentCheckedId);
        experimentProcessView.render();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        templateFragment = (NewExperimentTemplateFragment) fm.findFragmentByTag(NewExperimentTemplateFragment.class.getSimpleName());
        informationFragment = (NewExperimentInfoSettingFragment) fm.findFragmentByTag(NewExperimentInfoSettingFragment.class.getSimpleName());
        startFragment = (NewExperimentStartFragment) fm.findFragmentByTag(NewExperimentStartFragment.class.getSimpleName());
        hideAll(ft);
        switch (currentCheckedId) {
            case 0:
                if (templateFragment == null) {
                    templateFragment = new NewExperimentTemplateFragment(expInfo);
                    ft.add(R.id.container, templateFragment, NewExperimentTemplateFragment.class.getSimpleName());
                } else {
                    ft.show(templateFragment);
                }
                break;
            case 1:
                //基础设置
                if (informationFragment == null) {
                    informationFragment = new NewExperimentInfoSettingFragment(expInfo);
                    ft.add(R.id.container, informationFragment, NewExperimentInfoSettingFragment.class.getSimpleName());
                } else {
                    ft.show(informationFragment);
                }
                break;
            case 2:
                //程序设置
                if (programSettingFragment == null) {
                    programSettingFragment = new NewExperimentProgramSettingFragment(expInfo);
                    ft.add(R.id.container, programSettingFragment, NewExperimentProgramSettingFragment.class.getSimpleName());
                } else {
                    ft.show(programSettingFragment);
                }
                break;
            case 3:
                if (startFragment == null) {
                    startFragment = new NewExperimentStartFragment(expInfo);
                    ft.add(R.id.container, startFragment, NewExperimentStartFragment.class.getSimpleName());
                } else {
                    ft.show(startFragment);
                }
                break;
            default:
        }

        ft.commitAllowingStateLoss();
    }

    private void hideAll(FragmentTransaction ft) {
        if (templateFragment != null) {
            ft.hide(templateFragment);
        }
        if (informationFragment != null) {
            ft.hide(informationFragment);
        }
        if (programSettingFragment != null) {
            ft.hide(programSettingFragment);
        }
        if (startFragment != null) {
            ft.hide(startFragment);
        }
    }

    public ExpController getController() {
        return expController;
    }
}
